//***********************************************************************
//	File:		Anklebiter.cpp
//	Author:		
//	Course:		SGP 1405
//	Purpose:	
//***********************************************************************
//***********************************************************************
//	Purposfully left blank for all comments regarding this file 
//	
//	
//	
//***********************************************************************



#include "Anklebiter.h"
#include "ConcussionBlast.h"

#include "../SGD Wrappers/SGD_Event.h"
#include "../SGD Wrappers/SGD_GraphicsManager.h"

#include "../TileSystem/TileSystem.h"
#include "../GameStates/Play.h"



Anklebiter::Anklebiter(float posX, float posY) : SGD::Listener(this), Enemy(posX, posY)
{
	SetAcceleration({ 0 , 0 }); 
	SetAttackCDL(.5f); 
	SetAttackRange(500); 
	SetConfuseCDL(4); 
	SetConfuseTimer(0); 
	
	SetHealth(100); 
	//SetImage();
	SetKnockbackCDL(4); 
	SetKnockBackTimer(0);
	SetMass(90);
	SetMaxHealth(100);
	SetMaxSpeed(100);
	SetPassable(false);
	SetPosition(SGD::Point{ posX, posY }); 
	SetRootCDL(4); 
	SetRootTimer(0); 
	SetRotation(0);
	SetSize(SGD::Size{ 64,64 }); 
	SetSpeed(100);
	SetStaticFrictionCoefficient(0.7f);
	SetDynamicFrictionCoefficient(.03f); 
	SetDragForceCoefficient(0.04f);
	//SetTakeDamage() particle emitter
	SetValue(ENT_ANKLEBITER);
	SetVelocity(SGD::Vector{ 0, 0 }); 
	SetTarget ((Entity *)Play::GetInstance ()->GetPlayer () );
	RegisterForEvent("CONCUSSION_BLAST");
}

Anklebiter::~Anklebiter ()
{
	UnregisterFromEvent("CONCUSSION_BLAST");
	/*int * points = new int ( 5 );
	SGD::Event * pEvent = new SGD::Event ( "INCREASE_SCORE" , points , nullptr );
	pEvent->QueueEvent ( nullptr );
	pEvent = nullptr;*/
}

void Anklebiter::Update ( float dt )
{
	// If you are being pushed by a force then continue to react to it 
	// otherwise move as normal
	if ( GetForce ().x != 0.0f || GetForce ().y != 0.0f )
	{
		Moveable::Update(dt);
	}
	else
	{
		SetVelocity(SGD::Vector{0.0f,0.0f});
		//SGD::Vector distance;
		//// If you have a target go towards it
		//if ( GetTarget () != nullptr)
		//{
		//	distance.x = GetTarget()->GetPosition().x - m_ptPosition.x;
		//	distance.y = GetTarget()->GetPosition().y - m_ptPosition.y;

		//	float dist = distance.ComputeLength();

		//	if ( dist > 128 )

		//	{
		//		SGD::Vector direction = TILESYSTEM->GetPath ( this , GetTarget ()->GetPosition () );
		//		direction.x -= m_ptPosition.x;
		//		direction.y -= m_ptPosition.y;
		//		direction.Normalize();
		//		SGD::Vector rotation = SGD::Vector { 0 , -1 };
		//		float angle = direction.ComputeAngle ( rotation );

		//		if ( direction.ComputeLength () > 0.9f )
		//		{
		//			if ( rotation.ComputeSteering ( direction ) > 0 )
		//			{
		//				SetRotation ( angle );
		//			}
		//			else
		//			{
		//				SetRotation ( -angle );
		//			}
		//		}
		//	}
		//}
		//SGD::Vector newV = {0,-1};
		//newV.Rotate(GetRotation());
		//newV = newV * GetSpeed();
		//
		//SetVelocity(newV);
		////Last line of function
		//Entity::Update ( dt );
	}
}

void Anklebiter::Render ()
{
	SGD::GraphicsManager* draw = 
		SGD::GraphicsManager::GetInstance();

	SGD::Point cameraPos = Play::GetInstance()->GetCameraPos(); 

	SGD::Point offset = GetPosition() - GetSize() / 2.0f;
	offset.x -= cameraPos.x; 
	offset.y -= cameraPos.y;

	SGD::Rectangle render = GetRect();
	render.left -= cameraPos.x;
	render.right -= cameraPos.x;
	render.top -= cameraPos.y;
	render.bottom -= cameraPos.y;

	

	draw->DrawTextureSection(GetImage(), offset, SGD::Rectangle{ 0, 0, 64, 64 },
		GetRotation () , { GetSize ().width / 2 , GetSize().height / 2} , {255, 255, 255} , { 1.0f , 1.0f });
	draw->DrawRectangle(render, {0, 0,0,0}, {255, 0,0,0}, 2);
}

void Anklebiter::HandleCollision ( const IEntity* pOther )
{

}

void Anklebiter::HandleEvent (const SGD::Event* pEvent)
{
	if(pEvent->GetEventID() == "CONCUSSION_BLAST")
	{
		ConcussionBlast* blast = reinterpret_cast<ConcussionBlast*>(pEvent->GetSender());
		SGD::Vector distance =  GetPosition() - blast->GetPosition();
		float dist = distance.ComputeLength();
		if ( dist <= blast->GetEffectRadius () )
		{
			SGD::Vector force = distance.ComputeNormalized ();
			force *= blast->GetForce ();
			SetForce ( force );
		}
		
	}
}

SGD::Rectangle Anklebiter::GetRect () const
{
	SGD::Rectangle rect;
	
	rect.left = m_ptPosition.x - (m_szSize.width  * 0.5f);
	rect.top = m_ptPosition.y - (m_szSize.height  * 0.5f);
	rect.right = rect.left + m_szSize.width;
	rect.bottom = rect.top + m_szSize.height;

	return rect;
}

void Anklebiter::ModifyHealth ( float hpAmount )
{

}

void Anklebiter::ApplyFriction ( float dt )
{
	Moveable::ApplyFriction(dt);
}

void Anklebiter::Attack ( float dt )
{

}

void Anklebiter::Move ( void )
{
	
}